<?php
/* SVN FILE: $Id: delete.php 191 2009-09-01 22:19:51Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Delete Item page
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 191 $
* @lastrevision $Date: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/items/delete.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_items_delete extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		if(!is_array($this->caches['market_items'][$this->request['item_id']]))
		{
			$this->registry->output->showError(null);
		}
		
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'item_delete' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_items');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_items' ) );
		
		$execute = array(
							'form' => 'deleteForm',
							'delete' => 'deleteItem',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'form';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function deleteForm()
	{
		$this->registry->output->html = $this->html->deleteItemForm( $this->caches['market_items'][$this->request['item_id']] );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	public function deleteItem()
	{
		$item = $this->caches['market_items'][$this->request['item_id']];
		
		if($this->request['delete_option'])
		{
			$LIMIT = 100;
			$this->request['st'] = itnval($this->request['st']);
			
			$addMember = array();
			$this->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"item=".$this->ipsclass->input['item'],'order'=>'member_id ASC','limit'=>array($this->request['st'],$LIMIT)));
			$q = $this->DB->execute();
			while($inventoryItem = $this->DB->fetch($q))
			{
				if(!isset($addMember[$inventoryItem['member_id']]))
				{
					$addMember[$inventoryItem['member_id']] = 0;
				}
				if($this->request['delete_option'] == 1)
				{
					$addMember[$inventoryItem['member_id']] = numbers::add($addMember[$inventoryItem['member_id']],$inventoryItem['resell_price']);
				}
				elseif($this->request['delete_option'] == 2)
				{
					$addMember[$inventoryItem['member_id']] = numbers::add($addMember[$inventoryItem['member_id']],$inventoryItem['price_bought']);
				}
				elseif($this->request['delete_option'] == 3)
				{
					$addMember[$inventoryItem['member_id']] = numbers::add($addMember[$inventoryItem['member_id']],$this->caches['market_items'][$this->request['item_id']]['price']);
				}
			}
			
			if(count($addMember))
			{
				foreach($addMember as $memberID => $addMoney)
				{
					$this->DB->update('members',$this->settings['market_members_field']."=".$this->settings['market_members_field']."+".$addMoney,"member_id=".$memberID, false, true);
				}
				
				$this->registry->output->redirect($this->settings['base_url']."module=items&amp;section=delete&amp;do=delete&amp;item_id=".$this->request['item_id']."&amp;delete_option=".$this->request['delete_option']."&amp;st=".($this->request['st']+$LIMIT),$this->lang->words['delete_item_process_redirect']);
			}
		}
		
		marketRegistry::getItem($this->caches['market_items'][$this->request['item_id']])->adminRemove();
		$this->DB->delete('market_inventory',"item=".$this->request['item_id']);
		$this->DB->delete('market_items',"item_id=".$this->request['item_id']);
		
		$this->cache->rebuildCache('market_items','ibmarket');
		
		$this->lang->words['item_delete_log'] = str_replace("<#NAME#>",$item['item_name'],$this->lang->words['item_delete_log']);
		$this->lang->words['item_delete_log'] = str_replace("<#ID#>",$item['item_id'],$this->lang->words['item_delete_log']);
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['item_delete_log'] );
		
		$this->registry->output->global_message = $this->lang->words['item_deleted'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=items&amp;section=view&amp;category=".$item['category'] );
	}
}